package com.thinksns.sociax.t4.android.message;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.model.SystemMessageModel;
import com.thinksns.sociax.thinksnsbase.base.ListBaseAdapter;
import com.thinksns.sociax.thinksnsbase.exception.TimeIsOutFriendly;
import com.thinksns.sociax.thinksnsbase.utils.TimeHelper;

import java.util.List;

/**
 * @author LeiYan
 * @describe
 * @date 2017/7/26
 * @contact email:907238552@qq.com
 */

public class SystemMessageListAdapter extends ListBaseAdapter<SystemMessageModel> {
    private List<SystemMessageModel> mDataList;

    public SystemMessageListAdapter(Context context, List<SystemMessageModel> data) {
        super(context);
        this.mDataList = data;
    }

    public void setDataList(List<SystemMessageModel> mDataList) {
        this.mDataList = mDataList;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        ViewHolder holder;
        if (converView == null) {
            holder = new ViewHolder();
            converView = getLayoutInflater(mContext).inflate(R.layout.item_system_message, parent, false);
            holder.mTvContent = (TextView) converView.findViewById(R.id.tv_content);
            holder.mTvTime = (TextView) converView.findViewById(R.id.tv_time);
            converView.setTag(holder);
        } else {
            holder = (ViewHolder) converView.getTag();
        }
        SystemMessageModel data = getItem(position);
        /*过滤html标签 8.10.2017*/
        holder.mTvContent.setText(com.thinksns.sociax.thinksnsbase.utils.UnitSociax.filterHtml(data.getBody()));
        try {
            holder.mTvTime.setText(TimeHelper.friendlyTime((int) data.getCtime()));
        } catch (TimeIsOutFriendly timeIsOutFriendly) {
            timeIsOutFriendly.printStackTrace();
        }

        return converView;
    }

    @Override
    public int getMaxId() {
        return mDataList == null ? 0 : (int) mDataList.get(mDataList.size() - 1).getId();
    }

    @Override
    public SystemMessageModel getItem(int arg0) {
        return mDataList.get(arg0);
    }

    @Override
    public int getCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    class ViewHolder {
        TextView mTvContent;
        TextView mTvTime;
    }
}
